from smb.SMBConnection import SMBConnection

def connect_smb(username,password,ip,port=445):
    # https://pysmb.readthedocs.io/en/latest/api/smb_SMBConnection.html
    samba = SMBConnection(
        username, password, "", "", use_ntlm_v2=True
    )
    samba.connect(ip, port)
    samba.retrieveFile()
    return samba

# ================================================================
# 列出smb服务器下的所有共享目录（SharedDevice）:samba.listShares()
# https://pysmb.readthedocs.io/en/latest/api/smb_SharedDevice.html
# item.name是名称，item.isSpecial可以过滤掉形如"IPC$"的目录
# tuple(i.name for i in self.samba.listShares() if not i.isSpecial)

# ================================================================
# 遍历文件夹
# samba.listPath(service_name, dir_name)
# service_name: 服务名（smb中的文件夹名，一级目录）
# dir_name: 二级目录及以下的文件目录
# 每个SharedFile object的属性见https://pysmb.readthedocs.io/en/latest/api/smb_SharedFile.html
# tuple(
#         i.filename
#         for i in samba.listPath(service_name, dir_name)
#         if i.filename[0] != "."
#     )

# ================================================================
# 获取文件属性
# samba.getAttributes(service_name, file_path)
# service_name: 服务名（smb中的文件夹名，一级目录）
# dir_name: 二级目录及以下的文件目录
# 函数返回值是SharedFile object，属性见https://pysmb.readthedocs.io/en/latest/api/smb_SharedFile.html


# ================================================================
# 下载和上传
# samba.retrieveFile(service_name, path, file_obj, show_progress=False,tqdm_kargs={})
# samba.storeFile(service_name, path, file_obj, show_progress=False,tqdm_kargs={})
# file_obj是一个stream对象，默认支持tqdm进度条，可设置show_progress和tqdm_kargs

